/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.internal;

import java.util.EventListener;

import javax.servlet.http.Cookie;

import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.event.EventListenerList;
import nextapp.echo2.webcontainer.command.BrowserSetCookieCommand;

import org.apache.commons.lang.StringUtils;
import org.openeccos.EccosServlet;
import org.openeccos.util.ILookAndFeel;
import org.openeccos.util.PDUtil;

import echopointng.ButtonEx;
import echopointng.LabelEx;
import echopointng.PasswordFieldEx;
import echopointng.TextFieldEx;

/**
 *
 *
 * @author cgspinner@web.de
 */
public class PDLoginPanel extends Row {

	public final static String ECCOS_LOGIN_NAME = "ECCOS_LOGIN_NAME";
	
	private TextFieldEx txtLogin;
	private TextFieldEx txtPassword;
	private EventListenerList eventListenerList = new EventListenerList();
	private LabelEx lblMessage;
	private ButtonEx btnLogout;
	private Label lblPassword;
	private Label lblLogin;
	private Button btnEnter;

	public PDLoginPanel() {
		initGUI();
	}

	public void applyLookAndFeel(ILookAndFeel colorScheme) {
		lblLogin.setForeground(colorScheme.getForeGround());
		lblPassword.setForeground(colorScheme.getForeGround());
		btnEnter.setBackground(colorScheme.getBackgroundDark());
		lblMessage.setForeground(colorScheme.getForeGround());
		btnLogout.setBackground(colorScheme.getBackgroundDark());
		btnLogout.setForeground(colorScheme.getForeGround());
	}

	private void initGUI() {

		setCellSpacing(new Extent(6));

		Font smallFont = new Font(Font.ARIAL, Font.PLAIN, new Extent(11));

		lblLogin = new Label("Login");

		lblLogin.setFont(smallFont);
		add(lblLogin);

		txtLogin = new TextFieldEx();
		txtLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnEnterClicked();
			}
		});
		txtLogin.setFont(smallFont);
		txtLogin.setWidth(new Extent(85));
		txtLogin.setHeight(new Extent(14));
		add(txtLogin);

		//fill the name from the last session
		Cookie[] cookies = EccosServlet.getActiveConnection().getRequest().getCookies();
		for (Cookie cookie : cookies) {
			if (StringUtils.equals(cookie.getName(), ECCOS_LOGIN_NAME)) {
				txtLogin.setText(cookie.getValue());
				break;
			}
		}

		lblPassword = new Label("Password");

		lblPassword.setFont(smallFont);
		add(lblPassword);

		txtPassword = new PasswordFieldEx();
		txtPassword.setFont(smallFont);
		txtPassword.setMaximumLength(15);
		txtPassword.setWidth(new Extent(85));
		txtPassword.setHeight(new Extent(14));
		txtPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnEnterClicked();
			}
		});
		add(txtPassword);

		btnEnter = new Button(PDUtil.getImage("img/login.gif"));
		btnEnter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnEnterClicked();
			}
		});
		add(btnEnter);

		lblMessage = new LabelEx("");

		lblMessage.setVisible(false);
		add(lblMessage);

		btnLogout = new ButtonEx("Logout");

		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnLogoutClicked();
			}
		});
		btnLogout.setVisible(false);
		add(btnLogout);
		
		if (PDUtil.isEmpty(txtLogin.getText())) {
			ApplicationInstance.getActive().setFocusedComponent(txtLogin);
		} else {
			ApplicationInstance.getActive().setFocusedComponent(txtPassword);
		}
	}

	private void btnLogoutClicked() {
		LoginEvent evt = new LoginEvent(this, null, null, null);
		fireActionEvent(evt);
		lblMessage.setVisible(false);
		btnLogout.setVisible(false);
		txtLogin.setVisible(true);
		txtPassword.setVisible(true);
		lblLogin.setVisible(true);
		lblPassword.setVisible(true);
		btnEnter.setVisible(true);
	}

	private void btnEnterClicked() {
		LoginEvent evt = new LoginEvent(this, txtLogin.getText(), txtPassword.getText(), null);
		fireActionEvent(evt);
		lblMessage.setVisible(true);
		updateState(!evt.isLoginError(), evt.getLogin());
	}

	public void addActionListener(ActionListener listener) {
		eventListenerList.addListener(ActionListener.class, listener);
	}

	private void fireActionEvent(ActionEvent evt) {
		EventListener[] eventListeners = eventListenerList.getListeners(ActionListener.class);
		for (int i = 0; i < eventListeners.length; i++) {
			ActionListener listener = (ActionListener) eventListeners[i];
			listener.actionPerformed(evt);
		}
	}

	public void updateState(boolean isLoggedIn, String login) {
		if (isLoggedIn) {
			lblMessage.setText("You're logged in as: '" + login + "'");
			btnLogout.setVisible(true);
			txtLogin.setVisible(false);
			txtPassword.setVisible(false);
			lblLogin.setVisible(false);
			lblPassword.setVisible(false);
			btnEnter.setVisible(false);
			
			//store the login in a cookie
			BrowserSetCookieCommand bscc = new BrowserSetCookieCommand(new Cookie(ECCOS_LOGIN_NAME, txtLogin.getText()));
			ApplicationInstance.getActive().enqueueCommand(bscc);
		} else {
			txtPassword.setText("");
			lblMessage.setText("Error!");
		}
		lblMessage.setVisible(true);
	}
}
